const Seller = require('../model/Seller');
const Buller = require('../model/Buller');
const Good = require('../model/Good');
const Follow = require('../model/Follow');
const Like = require('../model/Likes');
const Desc = require('../model/Desc');
const Reply = require('../model/Reply');
const Message = require('../model/Message');


//验证模块
const validator = require('validator');
//db中有增加数据方法
const DbSet = require('../model/db');
const setting = require('../setting');
const auth = require('../common/auth');
const mail = require('../common/mail');
const multer = require('multer');


exports.goodInfo = (req,res,next)=>{
    var id = req.params.id;
    Good.getGoodById(id,(err,good)=>{
        if(err){
            return res.end(err);
        }

        return res.json({good})

    })

}

exports.plusCar = (req,res,next)=>{
    var id = req.body.good_id;
    var color = req.body.color;
    var model = req.body.model;
    var num = req.body.num;
    console.log(req.body);

    if(!req.session.buller){
        return res.end('fail');

    }else{


        Good.getGoodById(id,(err,good)=>{
            if(err){
                return res.end(err);
            }



            Buller.getBullerById(req.session.buller._id,(err,buller)=>{
                if(err){
                    return res.end(err);
                }


                var aa = good;
                aa.num = num;
                aa.color = color;
                aa.model = model;


                var cun = false;
               for (var i = 0;i< buller.shopCar.length;i++){

                   if(buller.shopCar[i]._id == id && buller.shopCar[i].color == color
                       && buller.shopCar[i].model == model){
                       cun = true;
                       buller.shopCar[i].num = buller.shopCar[i].num-0+num;
                       buller = new Buller(buller);
                       buller.save();
                   }

               }

               if(!cun){
                   buller.shopCar.push(aa);
                   buller.save();

               }
               req.session.buller = buller;
               res.end('success');


            })

        })

    }




}


exports.editCar = (req,res,next)=>{

    var id = req.query.id;
    var type = req.query.type;

    var king_id = req.session.buller._id;

    var color = req.query.color;
    var model = req.query.model;

    console.log(req.query.model)
    Buller.getBullerById(king_id,(err,buller)=>{
        if(err){
            return res.end(err);
        }


        for (var i = 0;i< buller.shopCar.length;i++){

            if(type == 'checkAll'){
                buller.shopCar[i].checkReady = true;
                buller = new Buller(buller);
                buller.save();
            }
            if(type == 'noCheckAll'){
                buller.shopCar[i].checkReady = false;
                buller = new Buller(buller);
                buller.save();
            }


            if(buller.shopCar[i]._id == id && buller.shopCar[i].color[0] == color
                && buller.shopCar[i].model[0] == model ){



               if(type == 'plus'){

                   buller.shopCar[i].num = buller.shopCar[i].num-0+1;
                   buller = new Buller(buller);

                   buller.save();

               }else if(type == 'minus') {
                   buller.shopCar[i].num = buller.shopCar[i].num-1;
                   buller = new Buller(buller);

                   buller.save();
               }else if(type == 'remove'){


                   buller.shopCar.splice(i,1);
                   buller = new Buller(buller);
                   buller.save();

               } else if(type == 'check'){


                   buller.shopCar[i].checkReady = !buller.shopCar[i].checkReady;
                   buller = new Buller(buller);
                   buller.save();

               }
            }

        }

        req.session.buller = buller;
        res.json({msg:'success',data:buller});



    })




}
exports.store = (req,res,next)=>{

    if(!req.session.buller){
        return res.end('fail');
    }
    var good_id = req.query.good_id;
    var king_id = req.session.buller._id;


    Like.getLikeById(good_id,king_id,(err,like)=>{

        if(err){
            return res.end(err);
        }
        if(like){

            Like.findOneAndRemove({'_id':like._id}).exec((function (err,data) {
                console.log(data)
            }));
            console.log(like)
        }else{
            var like = new Like({
                good_id,
                king_id
            })
            like.save();
        }
        res.end('success');


    })




    }

exports.checkStore = (req,res,next)=>{
    if(!req.session.buller){
        return res.end('fail');
    }

    var king_id = req.session.buller._id;
    var good_id = req.query.good_id;

    Like.getLikeById(good_id,king_id,(err,like)=>{
        if(err){
            return res.end(err);
        }

        if(!like){


            return res.json({store:false});
        }
        res.json({store:true});

    })


}
exports.desc = (req,res,next)=>{

    if(!req.session.buller){
        return res.end('fail');
    }
    req.body.king_id = req.session.buller._id;
    req.body.good_id = req.query.good_id;
    req.body.seller_id = req.query.seller_id;
    if(req.body.avatar){
        req.body.avatar = req.body.avatar.slice(9, req.body.avatar.length - 1);
        req.body.avatar = req.body.avatar.split(',');
    }else{
        req.body.avatar = []
    }


    var message1 = new Message({
        target_id:req.query.seller_id,
        content:req.body.question,
        send_id:req.session.buller._id,
        good_id:req.query.good_id,
        type:'seller'
    })
    console.log('aa')
    message1.save();


    var desc = new Desc(req.body);
    desc.save().then((desc)=>{

        Desc.getDescsByGood(req.query.good_id,(err,descs)=>{

            if(err){
                return res.end(err);
            }
            res.json({msg:'success',descs:descs});

        })

    });




}
exports.getDescs = (req,res,next)=>{
    var page = parseInt(req.query.page);
    if (!page) {
        page = 1;
    }


    Desc.find({'good_id':req.query.good_id}).populate('king_id').populate('seller_id').skip(2 * (page - 1)).limit(2).exec(function (err, descs) {
        if(err){
            return res.end(err);
        }

        Desc.getDescsByGood(req.query.good_id,(err,data)=>{
            if(err) {
            }
                res.json({count:data.length,descs});

        })



    })




}
exports.reply = (req,res,next)=>{
    if(!req.session.buller){
        return res.end('fail');
    }
    req.body.king_id = req.session.buller._id;
    var target_id = req.body.author_id;
    var content = req.body.content;
    var send_id = req.body.king_id;
    var good_id = req.body.good_id;

    if(send_id != target_id){
        var message = new Message({
            target_id,
            content,
            send_id,
            good_id,
            type:'buller'
        })
        message.save();
    }





    var reply = new Reply(req.body);
    reply.save().then((reply)=>{

        Reply.getReplysByGood(req.body.good_id,(err,replys)=>{

            if(err){
                return res.end(err);
            }
            res.json({msg:'success',replys:replys});

        })

    });

}

exports.getReplys = (req,res,next)=>{





        Reply.getReplysByGood(req.query.good_id,(err,data)=>{
            if(err) {
            }
            res.json({count:data.length,replys:data});

        })







}